/**
 *
 * Created by Pan,hongxue, 9/16/2011 4:53:15 PM
 * Modified by Song,YeWen, 10/10/2011 4:02:06 PM
 *
 */

function  validateVideoUrl(ori_url) 
{
     var dst_url;
	 var matches;
	 var html;
    /* we firstly use the specified vid in url */
    var vid_str = copyBetween(ori_url, 'vid=', '&');    
    
    if (vid_str)
        dst_url = 'http://vv.video.qq.com/getclist?otype=xml&vid=' + vid_str;
    else {
        /* we search the firstly matched url in the whole playlist */
        html = download_page(ori_url);
        matches = html.match(/vid\:"(.+?)"/);
        if (!matches) {
            // Not Found
    	  	return 'NotFoundVideo';
        }
        dst_url = 'http://vv.video.qq.com/getclist?otype=xml&vid=' + matches[1];
    }

    //try to fix the wrong vid surfixed by '_0'
    var surfix_len = dst_url.lastIndexOf("_");
    if(surfix_len != -1)
       dst_url = dst_url.substring(0,surfix_len);

    var xml_str = download_page(dst_url);
    var file_str= parse_xml(xml_str, 'vd', 'vi', 'fn');
    var url_str = parse_xml(xml_str, 'vd', 'vi', 'url');
    var flv_str = url_str + file_str;
    var duration = parse_xml(xml_str, 'vd', 'vi', 'dur');
    
    if(!html) html =  download_page(ori_url);
	//matches = html.match(/<title>(.*?)<\/title>/);
	matches = html.match(/<title>([\s\S]*?)<\/title>/);
    if(matches){
 	   return 'success_title:' + encodeURIComponent(matches[1])
 	          + '\n video_urls:' + encodeURIComponent(flv_str)
 	          + '\n seconds:' + Math.round(parseInt(duration));
    }
    return 'error:empty_path';
}

/* http://v.qq.com/cover/7/7dxcpwu7xlr2304.html?vid=8aV9lTLLyre */
function js_get_media_url(ori_url)
{
    var dst_url;
    var matches;
    /* we firstly use the specified vid in url */
    var vid_str = copyBetween(ori_url, 'vid=', '&');
    if (vid_str){
        //dst_url = 'http://vv.video.qq.com/getclist?otype=xml&vid=' + vid_str;
        dst_url ='http://vv.video.qq.com/getclip?vid='+vid_str+'&idx=1&fmt=shd&otype=json';
        var json = download_page(dst_url);
    }else {
        /* we search the firstly matched url in the whole playlist */
        var html = download_page(ori_url);
        matches = html.match(/vid\:"(.+?)"/);
        if (!matches) {
            // Not Found
            return '';
        }

        vid_str = matches[1];
        //dst_url = 'http://vv.video.qq.com/getclist?otype=xml&vid=' + matches[1];
    }

    var sb = [];
    for(var idx=1; idx < 20; idx++){
        dst_url ='http://vv.video.qq.com/getclip?vid='+vid_str+'&idx='+idx+'&fmt=shd&otype=json';
        var json = download_page(dst_url);
        alert("xxxxxxxxxxxxxxxxxxxxxxxjson:"+json);
        if(json.indexOf("idx is out of range") != -1) break;
        eval(json)
        var flv_str = "http://vliveachy.tc.qq.com/vlive.qqvideo.tc.qq.com/"+QZOutputJson.vi.fn+"?vkey="+QZOutputJson.vi.key+"&fmt=shd&rfc=v0";
      
        alert("xxxxxxxxxxxxxxxxxxxxxxxxxflv_str:"+flv_str);
        sb.push(flv_str);

        /*if (json && json.vd) {
            var cur = json.vd.vi[0];
            var files = cur.cl.ci;
            for (var i=0; i<files.length; i++) {
                var dot_pos = cur.fn.lastIndexOf('.');
                sb.push(cur.url + cur.fn.substr(0, dot_pos + 1) + files[i].idx + cur.fn.substr(dot_pos, dot_pos));
            }
        }*/
    }

    return sb;
}
